//
//  MessageAlertView.swift
//  ezyform
//
//  Created by Hubert on 2022/5/9.
//  Copyright © 2022 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
import HandyJSON

class MessageAlertView: UIView {
    private let alertWidth = APPConfig.WidthForPortrait - 36
    private let titleLabel = ContentLabel(R.font.interBold(size: 17))
    private let msgInfoTextView = BaseTextView()
    private let okBtn = ViewFactory.shared.generateButtonOnAlertView("View")
    private var mod: NotificationPushModel?
    var okBtnClosure: ((_ model: NotificationPushModel)->Void)?
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpView()
    }
    //for storyboard
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setUpView()
    }
    
    private func setUpView() {
        self.backgroundColor = UIColor.black.withAlphaComponent(0.5)
        //back view
        let backView = UIView()
        self.addSubview(backView)
        backView.backgroundColor = .white
        backView.layer.cornerRadius = Constant.BackViewCornerRadius
        backView.snp.makeConstraints { (make) in
            make.width.equalTo(alertWidth)
            make.height.lessThanOrEqualTo(500)
            make.center.equalToSuperview()
        }
        let topImageView = UIImageView()
        topImageView.image = UIImage.init(named: "message_alert_topBg")
        backView.addSubview(topImageView)
        topImageView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(84)
        }
       
        //title label
        titleLabel.textAlignment = NSTextAlignment.center
        titleLabel.numberOfLines = 2
        backView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(30)
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
        }
        //summary
        msgInfoTextView.font = R.font.interMedium(size: 15)
        msgInfoTextView.backgroundColor = .clear
        msgInfoTextView.textColor = UIColor(hexStr: "#292D32")
        msgInfoTextView.isEditable = false
        backView.addSubview(msgInfoTextView)
        msgInfoTextView.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(20)
            make.left.equalToSuperview().offset(24)
            make.height.equalTo(0)
            make.right.equalToSuperview().offset(-24)
        }
        //horizonal line view
        let horizonalLineView = View(hexStr: "#0000001A")
        backView.addSubview(horizonalLineView)
        horizonalLineView.snp.makeConstraints { (make) in
            make.top.equalTo(msgInfoTextView.snp.bottom).offset(15)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        backView.addSubview(okBtn)
        okBtn.snp.makeConstraints { (make) in
            make.top.equalTo(horizonalLineView.snp.bottom).offset(8)
            make.left.right.equalToSuperview()
            make.height.equalTo(40)
            make.bottom.equalToSuperview().offset(-8)
        }
        okBtn.addTarget(self, action: #selector(okBtnClick), for: .touchUpInside)
    }
    
    func setData(_ model: NotificationPushModel) {
        self.mod = model
        self.titleLabel.text = self.mod?.title
        self.msgInfoTextView.text = self.mod?.summary
        var msgHeight = self.msgInfoTextView.sizeThatFits(CGSize(width: self.alertWidth - 48, height: 0)).height
        if msgHeight > 334 {
            msgHeight = 334
        }
        self.msgInfoTextView.snp.updateConstraints { make in
            make.height.equalTo(msgHeight)
        }
    }

    @objc func okBtnClick(){
        self.removeFromSuperview()
        if let m = self.mod {
            self.okBtnClosure?(m)
        }
    }
}

